package fgoScript.entity.guda;

import commons.entity.Constant;
import commons.util.GameUtil;
import commons.util.PropertiesUtil;
import fgoScript.constant.GameConstant;
import fgoScript.constant.PointInfo;
import fgoScript.entity.*;
import fgoScript.service.CommonMethods;
import fgoScript.service.EventFactors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QpApGudaziForCaberSp extends AbstractApGudazi{
    private static final Logger LOGGER = LogManager.getLogger(QpApGudaziForCaberSp.class);

    @Override
    public void intoAndSelect(int apNum, int account) throws Exception {
        insertIntoTrainingRoomForSpecial(apNum, account);
    }

    @Override
    public void fightAndStop(boolean rebootFlag, int apNum, int account) throws Exception {
        // 1回合数字高亮点
        Point ps1 = new Point(903, 79);// 颜色：200;200;200 Color c = new Color(200, 200, 200);
        Point ps2 = new Point(903, 69);// 颜色：255;255;255 Color c = new Color(255, 255, 255);
        Point ps3 = new Point(904, 74);// 颜色：255;255;255 Color c = new Color(255, 255, 255);
        List<Point> pointList = new ArrayList<>();
        pointList.add(ps1);
        pointList.add(ps2);
        pointList.add(ps3);
        int eveValue;
        int battleRounds = 0;
        int MaxRounds = 20;
        int rounds = EventFactors.battleRounds;
        // 第一回合
        LOGGER.info("准备战斗，等待。。。。");
        CommonMethods.waitToAttack(null, LOGGER);
        LOGGER.info("第一回合开始：");
        boolean[] hases = {false,false};
        eveValue = GameUtil.getColorEveValue(pointList);
        if (rebootFlag) {
            DEFAULT_SKILL_COLORS = EventFactors.getDefaultSkillColorsFromFile();
        }else {
            DEFAULT_SKILL_COLORS = EventFactors.getSkillColors();
            EventFactors.writeDefaultSkillColors(DEFAULT_SKILL_COLORS);
        }
        String startegy;
        int count = 0;
        while (eveValue > GameConstant.THRESHOLD && battleRounds < MaxRounds) {
            battleRounds++;
			/*
			  连续使用技能组
			 */
            giveServantSkills(EventFactors.getPreSkills(DEFAULT_SKILL_COLORS));
            CommonMethods.waitToAttack(null, LOGGER);
            startegy = PropertiesUtil.getValueFromSkillsFile("SKILL_STRATEGY");
            if (count==0) {
                //加蓝放
                battleSkillForP02S01();
            }
            switch("".equals(startegy)? GameConstant.NO_SKILL : startegy){
                case GameConstant.NO_SKILL : {
                    CommonMethods.attackNPAAA(LOGGER);
                    break;
                }
                case GameConstant.GO_SECOD_SKILL_FOR_FIRST : {
                    attackBAAForEvent();
                    break;
                }
                case GameConstant.GO_ALL_SKILL_FOR_FIRST : {
                    attackBAAForEvent();
                    break;
                }
                default : {
                    break;
                }
            }
            // 等待
            CommonMethods.waitToAttack("1", LOGGER);
            eveValue = GameUtil.getColorEveValue(pointList);
            count++;
        }
        // 第二回合
        // 2回合数字高亮点
        pointList = new ArrayList<>();
        pointList.add(new Point(906, 70));
        pointList.add(new Point(900, 66));
        pointList.add(new Point(899, 81));
        CommonMethods.waitToAttack(null, LOGGER);
        LOGGER.info("第二回合开始：");
        eveValue = GameUtil.getColorEveValue(pointList);

        boolean hasSelect = false;
        count = 0;
        PointColor pc;
        boolean ifSecondNP = EventFactors.ifSecondNP;
        while (eveValue > GameConstant.THRESHOLD&& battleRounds < MaxRounds) {
            battleRounds++;
			/*
			  连续使用技能组
			 */
            giveServantSkills(EventFactors.getPreSkills(DEFAULT_SKILL_COLORS));
            pc = CommonMethods.waitToAttack(null, LOGGER);
            if ("attack".equals(pc.getName())) {
                attackBAAForEvent();
            }else {
                LOGGER.info("羁绊了，跳出来");
                break;
            }
            // 等待
            CommonMethods.waitToAttack("3", LOGGER);
            eveValue = GameUtil.getColorEveValue(pointList);
            count++;
        }
        if (rounds==3) {
            // 3回合数字高亮点
            pointList = new ArrayList<>();
            pointList.add(new Point(900, 66));
            pointList.add(new Point(903, 74));
            pointList.add(new Point(907, 79));
            pointList.add(new Point(897, 82));
            // 第三回合
            // 等待
            CommonMethods.waitToAttack(null, LOGGER);
            LOGGER.info("第三回合开始：");
            eveValue = GameUtil.getColorEveValue(pointList);
            hasSelect= false;
            count = 0;
            while (eveValue > GameConstant.THRESHOLD && battleRounds < MaxRounds) {
                battleRounds++;
                CommonMethods.waitToAttack("3", LOGGER);
				/*
				  连续使用技能组
				 */
                giveServantSkills(EventFactors.getPreSkills(DEFAULT_SKILL_COLORS));
                if (count==0) {
                    //加np
                    battleSkillForP02S03();
                }
                // 宝具平A
                attackBAAForEvent();
                CommonMethods.waitToAttack("3", LOGGER);
                if (checkFinish()) {
                    LOGGER.info("羁绊了，跳出来");
                    break;
                }
                eveValue = GameUtil.getColorEveValue(pointList);
                count++;
            }

        }
    }

    @Override
    public Point getSuppotServant() {
        Point p = null;
        try {
            p= Gates.getSuppPointByClassify(GameConstant.CLASSIFY_ASSASIN);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return p;
    }

    @Override
    public void fightOverMethod() {
    }
    @Override
    public String getSpecialGatesFilePath() {
        return System.getProperty("user.dir") + "/config/"+ Constant.FGO +"/special_all_qp.json";
    }

    public void insertIntoTrainingRoomForSpecial(int apNum, int acountNum) throws Exception {
        // 拉滚动条至最下上

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestDown(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpScrollRestTop(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 进入训练场
        // 周回进去
        Point p4 = PointInfo.getInstance().getP_WEEK_ENTRANCE();

        GameUtil.getMymouse().mouseMoveByPoint(p4,true,true);

        GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);

        GameUtil.delay(GameConstant.DELAY*5);

        // 拉滚动条至最下
        Point p16 = PointInfo.getInstance().getpScrollRestDown();

        GameUtil.getMymouse().mouseMoveByPoint(p16,true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 拖拽画面
        Point start = PointInfo.getInstance().getpDailySliceStrat();
        Point end = PointInfo.getInstance().getpDailySliceEnd();
        moveBySteps(start, end);

        // 点击日常
        Point p6 = PointInfo.getInstance().getpDailyEntrance();

        GameUtil.getMymouse().mouseMoveByPoint(p6,true,true);

        GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
        //获取入口信息
        GatesInfo gi = getSpecialGatesInfo();

        List<Gates> gatesList = gi.getGatesArray();
        int size = gatesList.size();
        Gates gatesTemp;
        Point locPoint;
        Point gatePoint;
        for (int i = 0; i < size; i++) {
            gatesTemp = gatesList.get(i);
            locPoint = gatesTemp.getpSetLoc();
            gatePoint = gatesTemp.getGateByApNum(apNum);
            // 拉滚动条至最远点
            double minusDown = Math.abs(locPoint.getY() - gi.getSliceDownPoint().getY());
            double minusUp = Math.abs(locPoint.getY() - gi.getSliceTopPoint().getY());
            if (minusDown < minusUp) {

                GameUtil.getMymouse().mouseMoveByPoint(gi.getSliceTopPoint(),true,true);

            }else{

                GameUtil.getMymouse().mouseMoveByPoint(gi.getSliceDownPoint(),true,true);

            }
            GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
            // 拉滚动条目标位置

            GameUtil.getMymouse().mouseMoveByPoint(locPoint,true,true);

            GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
            // 点击ap本

            GameUtil.getMymouse().mouseMoveByPoint(gatePoint,true,true);

            GameUtil.getMymouse().mousePressAndReleaseForConfirm(null);
            break;
        }
    }
    /**
     * 宝具平a攻击
     *
     * @throws Exception 异常
     */
    private void attackBAAForEvent() throws Exception {
        Point pNp01 = PointInfo.getInstance().getNpCheck();
        Point pNp02 = new Point((int)pNp01.getX()+ GameConstant.HEAD_SPACE, (int)pNp01.getY());
        Color cNp02 = GameUtil.getScreenPixel(pNp02);
        GameUtil.delay(100);
        Color cNp02_Delay = GameUtil.getScreenPixel(pNp02);
        int npCount = 0;
        boolean hasNp02 = cNp02.getRGB() != cNp02_Delay.getRGB();
        boolean has = hasNp02;
        LOGGER.info("有宝具吗？  " + (has));
        if ( has ) {
            // 等待
            CommonMethods.waitToAttack(null, LOGGER);
            // 蓝色圆板选择
            CommonMethods.blueAttackSelect();
            // 开始点击卡片
            Map<String, List<CommonCard>> scMap;
            scMap = CommonMethods.getWeakCommondCards(CardComparator.getBgrComparotor(), LOGGER);
            Point np_np01 = PointInfo.getInstance().getNpNp();
            Point np_np02 = new Point((int) np_np01.getX()+GameConstant.NP_SPACE,(int) np_np01.getY());
            if (hasNp02) {
                // 宝具

                GameUtil.getMymouse().mouseMoveByPoint(np_np02,true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
                npCount++;
            }
            List<CommonCard> trueList = scMap.get("trueList");
            List<CommonCard> falseList = scMap.get("falseList");
            int size = trueList.size();
            CommonCard commonCard;
            for (int i = 0; i < size; i++) {
                commonCard = trueList.get(i);

                GameUtil.getMymouse().mouseMoveByPoint(commonCard.getpLoc(),true,true);

                GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
            }
            if (size < (3-npCount)) {
                int num = 2-trueList.size()+1;
                for (int j = 0; j < num; j++) {

                    GameUtil.getMymouse().mouseMoveByPoint(falseList.get(j).getpLoc(),true,true);

                    GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
                }
            }
        } else {
            CommonMethods.attackNPAAA(LOGGER);
        }
        CommonMethods.checkExitCardSelect();
    }
    private void battleSkillForP01() throws Exception {
        // 等待
        CommonMethods.waitToAttack(null, LOGGER);
        // 战斗服选择

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpCloth(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 选择技能1

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpClothSkill01(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
        // 选择人物

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getSkillPerson01(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);

    }
    private void battleSkillForP02S03() throws Exception {
        // 等待
        CommonMethods.waitToAttack(null, LOGGER);
        // 战斗服选择

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpCloth(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 选择技能2

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpClothSkill03(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
        // 选择人物

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getSkillPerson02(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
    }
    private void battleSkillForP02S01() throws Exception {
        // 等待
        CommonMethods.waitToAttack(null, LOGGER);
        // 战斗服选择

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpCloth(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/4);
        // 选择技能3

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getpClothSkill01(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
        // 选择人物

        GameUtil.getMymouse().mouseMoveByPoint(PointInfo.getInstance().getSkillPerson02(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(GameConstant.DELAY/6);
    }
}
